<template>
  <el-dialog
      top="5vh"
      width="1000px"
      title="添加/编辑公告"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'NoticeAdd',
  data() {
    return {
      apiKey: 'notice',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        title: '',
        position: 'interview',
        content: '',
        status: 1,
        sort: 0,
        start_time: null,
        end_time: null,
        view_count: 0,
        is_top: 0,
        is_recommend: 0
      },
      rules: {
        title: [{required: true, message: '公告标题不能为空'}],
        position: [{required: true, message: '公告位置不能为空'}],
        content: [{required: true, message: '公告内容不能为空'}],
      },
      formList: [
        {
          label: '公告标题',
          prop: 'title',
          name: 'input',
          colSpan: 24,
          placeholder: '请输入公告标题'
        },
        {
          label: '公告位置',
          prop: 'position',
          name: 'select',
          colSpan: 12,
          placeholder: '请选择公告位置',
          options: [
            { value: 'interview', label: '首页' },
            // { value: 'written', label: '笔试' }
          ]
        },
        {
          label: '排序',
          prop: 'sort',
          name: 'number',
          colSpan: 12,
          min: 0,
          placeholder: '数字越小越靠前'
        },
        {
          label: '公告内容',
          prop: 'content',
          name: 'textarea',
          colSpan: 24,
          rows: 8,
          placeholder: '请输入公告内容'
        },
        {
          label: '开始时间',
          prop: 'start_time',
          name: 'datetime',
          colSpan: 12,
          type: 'datetime',
          placeholder: '请选择开始时间（可选）'
        },
        {
          label: '结束时间',
          prop: 'end_time',
          name: 'datetime',
          colSpan: 12,
          type: 'datetime',
          placeholder: '请选择结束时间（可选）'
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          colSpan: 8,
          placeholder: '请选择状态',
          options: [
            { value: 1, label: '启用' },
            { value: 0, label: '禁用' }
          ]
        },
        {
          label: '是否置顶',
          prop: 'is_top',
          name: 'select',
          colSpan: 8,
          placeholder: '请选择是否置顶',
          options: [
            { value: 1, label: '是' },
            { value: 0, label: '否' }
          ]
        },
        {
          label: '是否推荐',
          prop: 'is_recommend',
          name: 'select',
          colSpan: 8,
          placeholder: '请选择是否推荐',
          options: [
            { value: 1, label: '是' },
            { value: 0, label: '否' }
          ]
        },
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      // 处理表单变化
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, this.ruleForm).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
